<template>
  <div>
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="可读的应用">
        <el-checkbox-group v-model="allow_app_read">
          <el-checkbox v-for="i in applistall" :label="i.id" :key="i.id">{{i.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="资金类型">
        <el-radio-group v-model="form.fund_type">
          <el-radio v-model="form.fund_type" label="money">人民币</el-radio>
          <el-radio v-model="form.fund_type" label="currency">亚数币</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="资金数量" prop="fund">
        <el-input class="w500" v-model="fund" placeholder="例如 人民币(元)：200或者亚数币(个): 200"></el-input>
      </el-form-item>
      <el-form-item label="积分数量" prop="integral">
        <el-input class="w500" v-model="form.integral" placeholder="例如 积分(个): 1" disabled></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input class="w500" v-model="form.remark" placeholder="200元赠送1积分"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data: function () {
      return {
        form: {
          fund_type: 'money',
          fund: 0,
          other_app_read: '',
          is_public: 0,
          integral: 1,
          remark: ''
        },
        fund: '',
        allow_app_read: [11]
      }
    },
    computed: {
      ...mapState({
        applistall: state => state.app.all
      })
    },
    created () {
      this.appAll()
    },
    methods: {
      // 获取不分页的应用列表
      async appAll () {
        await this.$store.dispatch('app/appAll')
      },
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.form.fund_type === 'money') {
              this.form.fund = Number(this.fund) * 100
            } else {
              this.form.fund = Number(this.fund)
            }
            this.form.other_app_read = this.allow_app_read.join(',')
            this.$store.dispatch('certificate/distributeRuleAdd', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/certificate/distributerule')
            })
          }
        })
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
